package com.bridgeintelligent.tag.customer.service;

import com.bridgeintelligent.tag.customer.entity.AnalysisSetup;
import com.bridgeintelligent.tag.customer.mapper.AnalysisCatalogMapper;
import com.bridgeintelligent.tag.customer.mapper.AnalysisQuerySetupMapper;
import com.bridgeintelligent.tag.customer.mapper.ThemeMapper;
import com.bridgeintelligent.tag.customer.theme.ThemeRelation;
import com.bridgeintelligent.tag.user.mgmt.pojo.User;
import com.bridgeintelligent.tag.utils.DateHelper;
import com.bridgeintelligent.tag.webserver.security.TagSecurityHelper;
import com.wayneleo.quickstart.framework.IDGenerator;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 画像分析服务层接口实现
 */
@Service
@Transactional(rollbackFor = Exception.class)
@AllArgsConstructor
@Slf4j
public class ThemeServiceImpl implements IThemeService {

    private ThemeMapper themeMapper;

    private AnalysisQuerySetupMapper querySetupMapper;

    private AnalysisCatalogMapper catalogMapper;

    private IAnalysisQuerySetupService analysisQuerySetupService;

    /**
     * 新增用户主题标签关系
     * @param theme
     */
    @Override
    public void addTheme(ThemeRelation theme) {
        User user = TagSecurityHelper.currentUser();
        theme.setId(IDGenerator.gen())
                .setUserId(user.getUserId())
                .setCreateTime(DateHelper.currentDateTime())
                .setUpdateTime(DateHelper.currentDateTime());
        //保存关系信息
        themeMapper.addTheme(theme);
        //保存分析设置信息
        List<AnalysisSetup> setups = theme.getSetups();
        if (!CollectionUtils.isEmpty(setups)){
            setups.forEach(e ->{
                e.setId(IDGenerator.gen());
                e.setTpId(theme.getId());
                querySetupMapper.addQuerySetup(e);
            });
        }

    }


    /**
     * 修改用户主题标签关系
     * @param theme
     */
    @Override
    public void updateTheme(ThemeRelation theme) {
        User user = TagSecurityHelper.currentUser();
        theme.setUpdateTime(DateHelper.currentDateTime()).setUserId(user.getUserId());
        themeMapper.updateTheme(theme);

        analysisQuerySetupService.delByTpId(theme.getId());
        //修改分析设置信息
        List<AnalysisSetup> setups = theme.getSetups();
        if (!CollectionUtils.isEmpty(setups)){
            setups.forEach(e ->{
                e.setId(IDGenerator.gen());
                e.setTpId(theme.getId());
                querySetupMapper.addQuerySetup(e);
            });
        }
    }


    /**
     * 删除画像分析
     * @param id
     */
    @Override
    public void deleteTheme(String id) {
        themeMapper.deleteTheme(id);
        querySetupMapper.delQuerySetupByTpId(id);
    }

    /**
     * 画像分析详情
     * @param id
     * @return
     */
    @Override
    public ThemeRelation findThemeRelation(String id) {
        ThemeRelation theme = themeMapper.findThemeRelation(id);
        List<AnalysisSetup> setups = querySetupMapper.findQueryByTpId(id);
        theme.setSetups(setups);
        return theme;


    }

}
